from django.db import models


# Create your models here.

class Project(models.Model):
    """项目表"""
    name = models.CharField(max_length=128, blank=False, null=False, verbose_name="项目名称")
    name_en = models.CharField(max_length=64, blank=False, null=False, verbose_name="项目目录名")
    describe = models.CharField(max_length=512, blank=True, null=True, verbose_name="项目描述")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "项目表"
        db_table = "project"


class SelectDefaultProject(models.Model):
    """自动选择上次选中项目"""
    select_project_id = models.IntegerField(blank=True, null=False, verbose_name="已选择的项目id")
    b_user = models.ForeignKey("User", on_delete=models.CASCADE, verbose_name="属于哪个用户")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.select_project_id

    class Meta:
        verbose_name_plural = "自动选择上次选中项目"
        db_table = "selectdefaultproject"


class UserType(models.Model):
    """用户类型表"""
    name = models.CharField(max_length=64, blank=False, null=False, verbose_name="用户类型")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "用户类型表"
        db_table = "usertype"


class MyAuthType(models.Model):
    """认证类型"""
    auth_type = models.CharField(max_length=32, blank=False, null=False, verbose_name="认证类型")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.auth_type

    class Meta:
        verbose_name_plural = "认证类型"
        db_table = "myauthtype"


class User(models.Model):
    """用户表"""
    name = models.CharField(max_length=64, blank=False, null=False, verbose_name="用户名")
    password = models.CharField(max_length=64, blank=False, null=False, verbose_name="密码")
    usertype = models.ForeignKey("UserType", on_delete=models.CASCADE, verbose_name="用户类型")
    phone = models.CharField(max_length=64, blank=True, null=True, verbose_name="联系号码")
    phone_bak = models.CharField(max_length=64, blank=True, null=True, verbose_name="备用联系号码")
    project = models.ManyToManyField("Project", verbose_name="用户负责的项目")
    motto = models.CharField(max_length=512, blank=True, null=True, verbose_name="座右铭")
    hobby = models.CharField(max_length=512, blank=True, null=True, verbose_name="兴趣爱好")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "用户表"
        db_table = "user"

class OpsLogger(models.Model):
    """操作记录表"""
    log_user = models.CharField(max_length=32, blank=True, null=False, verbose_name="操作人")
    log_time = models.DateTimeField(auto_now_add=True, verbose_name="操作时间")
    log_ops = models.CharField(max_length=512, blank=True, null=False, verbose_name="操作内容")

    def __str__(self):
        return self.log_ops

    class Meta:
        verbose_name_plural = "操作记录表"
        db_table = "opslogger"

class DevRecord(models.Model):
    """开发记录表"""
    content = models.CharField(max_length=512, blank=True, null=True, verbose_name="开发内容")
    name = models.CharField(max_length=32, blank=True, null=True, verbose_name="开发人")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="开发时间")

    def __str__(self):
        return self.content

    class Meta:
        verbose_name_plural = "开发记录表"
        db_table = "devrecord"


class Document(models.Model):
    """运维文档表"""
    title = models.CharField(max_length=512, blank=True, null=False, verbose_name="标题")
    content = models.TextField(blank=True, null=True, verbose_name="内容")
    b_user = models.ForeignKey("User", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="属于哪个用户")
    b_project = models.ForeignKey("Project", on_delete=models.CASCADE, verbose_name="属于哪个项目")
    see_range = models.CharField(max_length=128, blank=True, null=True, verbose_name="可见范围")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "运维文档表"
        db_table = "document"


class SelectTheme(models.Model):
    """主题"""
    theme_number = models.IntegerField(default=0, verbose_name="ssh端口")
    user = models.ForeignKey("User", null=True, blank=True, on_delete=models.SET_NULL, verbose_name="所属用户")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.theme_number

    class Meta:
        verbose_name_plural = "主题"
        db_table = "selecttheme"
